<template>
  <div style="margin-top: 20px">
    <el-card class="box-card">
      <div slot="header" class="header">
        <!-- 左侧内容 -->
        <div class="left">
          <!-- @tab-click="handleClick"  -->
          <el-tabs v-model="activeName">
            <el-tab-pane label="销售额" name="first"></el-tab-pane>
            <el-tab-pane label="访问量" name="second"></el-tab-pane>
          </el-tabs>
        </div>
        <div class="right">
          <span @click="today">今日</span>
          <span @click="week">本周</span>
          <span @click="month">本月</span>
          <span @click="year">本年</span>

          <el-date-picker
            v-model="date"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </div>
      </div>
      <div class="container">
        <el-row>
          <el-col :span="18">
            <div class="echarts" ref="echarts"></div>
          </el-col>
          <el-col :span="6">
            <div class="list">
              <p>门店{{ title }}排名</p>
              <ul>
                <li>
                  <span class="top">1</span>
                  <span>麦当劳</span>
                  <span class="number">222，333</span>
                </li>
                <li>
                  <span class="top">2</span>
                  <span>麦当劳</span>
                  <span class="number">222，333</span>
                </li>
                <li>
                  <span class="top">3</span>
                  <span>麦当劳</span>
                  <span class="number">222，333</span>
                </li>
                <li>
                  <span>4</span>
                  <span>麦当劳</span>
                  <span class="number">222，333</span>
                </li>
                <li>
                  <span>5</span>
                  <span>麦当劳</span>
                  <span class="number">222，333</span>
                </li>
                <li>
                  <span>6</span>
                  <span>麦当劳</span>
                  <span class="number">222，333</span>
                </li>
                <li>
                  <span>7</span>
                  <span>麦当劳</span>
                  <span class="number">222，333</span>
                </li>
              </ul>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
// 导入echarts
import * as echarts from "echarts";
import dayjs from "dayjs";
import { mapState } from "vuex";
export default {
  data() {
    return {
      activeName: "first",
      date: "",
      myecharts: null,
    };
  },
  mounted() {
    // console.log(echarts);
    //   初始化echarts
    this.myecharts = echarts.init(this.$refs.echarts);
    // 配置
    this.myecharts.setOption({
      title: {
        text: this.activeName === "first" ? "销售额趋势" : "访问量趋势",
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          // data: listState.list.userFullYearAxis,
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          // data: [10, 52, 200, 334, 390, 330, 220, 330, 220, 330, 220, 88],
        },
      ],
    });
  },
  methods: {
    today() {
      let day = dayjs().format("YYYY-MM-DD");
      this.date = [day, day];
    },
    week() {
      let start = dayjs().day(0).format("YYYY-MM-DD");
      let end = dayjs().day(6).format("YYYY-MM-DD");
      this.date = [start, end];
    },
    month() {
      let start = dayjs().startOf("month").format("YYYY-MM-DD");
      let end = dayjs().endOf("month").format("YYYY-MM-DD");
      this.date = [start, end];
    },
    year() {
      const start = dayjs().startOf("year").format("YYYY-MM-DD");
      const end = dayjs().endOf("year").format("YYYY-MM-DD");
      this.date = [start, end];
    },
  },
  computed: {
    title() {
      return this.activeName == "first" ? "销售额" : "访问量";
    },
    ...mapState({
      listState: (state) => state.home.list,
    }),
  },
  watch: {
    activeName() {
      this.myecharts.setOption({
        title: {
          text: this.activeName === "first" ? "销售额趋势" : "访问量趋势",
        },
        xAxis: [
          {
            data:
              this.activeName === "first"
                ? this.listState.orderFullYearAxis
                : this.listState.userFullYearAxis,
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",

            data:
              this.activeName === "first"
                ? this.listState.orderFullYear
                : this.listState.userFullYear,
          },
        ],
      });
    },
    listState() {
      this.myecharts.setOption({
        xAxis: [
          {
            type: "category",
            data: this.listState.userFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: this.listState.orderFullYear,
          },
        ],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.right > span {
  padding: 0 10px;
}
.echarts {
  width: 100%;
  height: 500px;
}
.list {
  height: 500px;
}
.list > ul {
  list-style-type: none;
}
.list > ul > li {
  height: 70px;
  //   line-height: 50px;
  width: 100%;
}
li > span {
  margin-right: 20px;
  display: inline-block;
  text-align: center;
}
.top {
  width: 20px;
  height: 20px;
  line-height: 20px;
  border-radius: 50%;
  background: #000;
  color: #fff;
}
.number {
  float: right;
}
</style>
